package net.mysoft.budgetsoft.system.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;

import net.mysoft.budgetsoft.system.dao.DictionaryDetailDao;
import net.mysoft.budgetsoft.system.domain.TDictionaryDetail;
import net.mysoft.budgetsoft.system.pagemodel.DictionaryDetail;
import net.mysoft.budgetsoft.system.service.DictionaryDetailService;
import net.mysoft.framework.model.DataGrid;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

/**   
 *  
 * @author dagongPGP v1.0 
 * @link <a href=http://www.dagongsoft.net>北京大公信软科技有限公司</a> 
 *  
 * @version $Revision: 1.00  $Date: 2013-02-06 11:01:16 
 */
@Service("dictionaryDetailService")
public class DictionaryDetailServiceImpl implements DictionaryDetailService{
	@Resource
	private DictionaryDetailDao dictionaryDetailDao;
	
	public DictionaryDetailDao getDictionaryDetailDao(){
		return this.dictionaryDetailDao;
	}
	
	public void setDictionaryDetailDao(DictionaryDetailDao dictionaryDetailDao){
		this.dictionaryDetailDao = dictionaryDetailDao;
	}
	
	@Override
	public void add(DictionaryDetail dictionaryDetail){
		TDictionaryDetail t = new TDictionaryDetail();
		BeanUtils.copyProperties(dictionaryDetail, t);
		t.setDetailID(UUID.randomUUID().toString());
		dictionaryDetailDao.save(t);
	}
	
	@Override
	public void edit(DictionaryDetail dictionaryDetail){
		TDictionaryDetail t = new TDictionaryDetail();
		BeanUtils.copyProperties(dictionaryDetail, t);
		dictionaryDetailDao.update(t);
	}
	
	@Override
	public void delete(DictionaryDetail dictionaryDetail) {
		for (String id : dictionaryDetail.getIds().split(",")) {
			Object t =  dictionaryDetailDao.get(TDictionaryDetail.class, id);
			if (t != null) {
				 dictionaryDetailDao.delete(t);
			}
		}
	}
	
	@Override
	public DataGrid datagrid(DictionaryDetail dictionaryDetail){
		DataGrid dg = new DataGrid();
		String hql = "from TDictionaryDetail t where 1=1 ";
		List params = new ArrayList();
		hql = addWhere(dictionaryDetail, hql, params);
		String totalHql = "select count(*) " + hql;
		hql = addOrder(dictionaryDetail, hql);
		List<TDictionaryDetail> toList = dictionaryDetailDao.find(hql, params, dictionaryDetail.getPage(), dictionaryDetail.getRows());
		List<DictionaryDetail> oList = new ArrayList<DictionaryDetail>();
		oList = changeModel(toList);
		dg.setTotal(dictionaryDetailDao.count(totalHql, params));
		dg.setRows(oList);
		return dg;
	}
	
	@Override
	public List<DictionaryDetail> find(DictionaryDetail dictionaryDetail) {
		List<Object> params = new ArrayList();
		String hql = addOrder(dictionaryDetail,addWhere(dictionaryDetail,"from TDictionaryDetail t where 1=1 ",params));
		List<TDictionaryDetail> toList = dictionaryDetailDao.find(hql,params);
		List<DictionaryDetail> oList = new ArrayList<DictionaryDetail>();
		oList = changeModel(toList);
		return oList;
	}
	
	private String addWhere(DictionaryDetail dictionaryDetail, String hql, List<Object> params) {
		if (dictionaryDetail.getTypeID() != null) {
			hql += " and t.type.typeID=? ";
			params.add(dictionaryDetail.getTypeID());
		}
		if(dictionaryDetail.getNote()!=null&&!"".equals(dictionaryDetail.getNote())) {
			hql += " and t.note=? ";
			params.add(dictionaryDetail.getNote());
		}
		return hql;
	}
	
	private String addOrder(DictionaryDetail dictionaryDetail, String hql) {
		if (dictionaryDetail.getSort() != null) {
			hql += " order by " + dictionaryDetail.getSort() + " " + dictionaryDetail.getOrder();
		}
		return hql;
	}
	
	public List<DictionaryDetail> changeModel(List<TDictionaryDetail> tDictionaryDetail ){
		List<DictionaryDetail> list = new ArrayList<DictionaryDetail>();
		for(TDictionaryDetail to:tDictionaryDetail){
			DictionaryDetail o = new DictionaryDetail();
			BeanUtils.copyProperties(to, o);
			list.add(o);
		}
		return list;
	}


}
